package com.jz.netthings.utils;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * @author yyy
 * @ClassName GeneratorServiceEntity
 * @Description 自动生成代码工具类
 * @date 2018/8/1 14:00
 */
public class GeneratorServiceEntity {
    @Test
    public void generateCode() {
        String packageName = "com.jz.netthings.bloodsources";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName,
                "xy_link"
        );
//        generateByTables(serviceNameStartWithI, packageName, "hc_plan_after_final_assembly", "hc_plan_sale",
//                "hc_plan_make_part", "hc_plan_machine", "hc_plan_final_assembly","hc_plan_after_final_assembly_example");
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
         String dbUrl = "jdbc:mysql://192.168.0.99:3306/jz_netthings";
        // String dbUrl = "jdbc:sqlserver://192.168.0.153:1433;databaseName=Plasmstation";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
              //dataSourceConfig.setDbType(DbType.SQL_SERVER)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("fan4210")
               .setDriverName("com.mysql.jdbc.Driver");
        // .setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(false)
                .setAuthor("张凡")
                .setOutputDir("D:\\自动生成的代码")
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("bean")
                ).execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}
